package com.by.service.impl;

import com.by.pojo.ShopCart;
import com.by.service.CartService;
import com.by.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @version 1.0
 * @Author 彭彦彬
 * @Date 2025/4/16 20:22
 * @注释
 */
@Service
public class CartServiceImpl implements CartService {
    @Autowired
    RedisUtils redisUtils;

    @Override
    public void add(String userId, ShopCart shopCart) {
        redisUtils.addHash(userId, shopCart.getProductId(), shopCart.getNumber() + "");
    }

    @Override
    public void upCart(String userId, String productId, String number) {
        if (Integer.parseInt(number) > 0) {
            redisUtils.addHash(userId, productId, number);
        } else {
            redisUtils.deleteHash(userId, productId);
        }
    }

    @Override
    public Map<String, String> getUserCart(String userId) {
        return redisUtils.getUserCart(userId);
    }

    @Override
    public int delShopCart(String userId, String productId) {
        if ((redisUtils.getHash(userId, productId)) == null) {
            return -1;
        } else {
            redisUtils.deleteHash(userId, productId);
            return 1;
        }
    }
}
